package com.topnav.mc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.topnav.mc.admin.dao.mapper.PermissionMapper;
import com.topnav.mc.admin.entity.Permission;
import com.topnav.mc.admin.entity.Role;
import com.topnav.mc.admin.entity.RolePermission;
import com.topnav.mc.admin.service.IPermissionService;
import com.topnav.mc.admin.service.IRolePermissionService;
import com.topnav.mc.base.CommServiceImpl;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionServiceImpl extends CommServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private IRolePermissionService rolePermissionService;

    @Override
    public boolean beforeDelete(Permission entity){
        LambdaQueryWrapper<RolePermission> rolePermissionWrapper = new LambdaQueryWrapper<>();
        rolePermissionWrapper.eq(RolePermission::getPermissionId,entity.getId());
        if (rolePermissionService.getOne(rolePermissionWrapper,false) != null){
            throw ExceptionUtil.commonException(ExceptionMsg.ERROR_DELETE_PERMISSION_USED_BY_ROLE);
        }
        return true;
    }
    /**
     * 查询所有的权限
     * @return
     */
    @Override
    public List<Permission> findAllPermission() {
        return permissionMapper.findAllPermission();
    }

    /** 查某人的权限 */
    @Override
    public List<Permission> findPermissionByUserId(Long userId) {
        return permissionMapper.findPermissionByUserId(userId);
    }
}
